*********************************************************************************************************************
*********************************** Recoding of individual level BHPS data ******************************************
*********************************************************************************************************************
		use "BHPS_raw.dta", replace
		
		
*********************** SET AS PANEL DATA **************************************
********************************************************************************
		tsset pid waven
		sort pid waven

		gen year=1990+waven
		
		
************************ RESIDENCE IN LOCAL AREA DISTRICT **********************
********************************************************************************		
		codebook oslaua // 405 Local Area Districts observed
		clonevar lad13=oslaua


************************ ATTITUDES (OUCTOME VARIABLES) *************************
********************************************************************************
**** recode NATIONAL IDENTITY items
		* People have different views about Britain today. I am going to read out a list of some things that have been said about Britain and I'd like you to tell me which answer off the card comes closest to how you feel
		recode opnata (-9=.) (-8=.) (-7=.) (-1=.) (1=4) (2=3) (3=2) (4=1) (5=0), gen(nat1) // Britain has a lot to learn from other countries in running its affairs
		recode opnatb (-9=.) (-8=.) (-7=.) (-1=.) (1=4) (2=3) (3=2) (4=1) (5=0), gen(nat2) // I would rather be a citizen of Britain than of any other country in the world
		recode opnatc (-9=.) (-8=.) (-7=.) (-1=.) (1=4) (2=3) (3=2) (4=1) (5=0), gen(nat3) // There are some things about Britain today that make me ashamed to be British
		recode opnatd (-9=.) (-8=.) (-7=.) (-1=.) (1=4) (2=3) (3=2) (4=1) (5=0), gen(nat4) // People in Britain ere too ready to criticise their country
		recode opnate (-9=.) (-8=.) (-7=.) (-1=.) (1=4) (2=3) (3=2) (4=1) (5=0), gen(nat5) // The government should do everything it can to keep all parts of Britain together in a single state
		recode opnatf (-9=.) (-8=.) (-7=.) (-1=.) (1=4) (2=3) (3=2) (4=1) (5=0), gen(nat6) // Britain should co-operate with other countries, even if it means giving up some independence

		label variable nat1 "Britain has a lot to learn from other countries in running its affairs"
		label variable nat2 "I would rather be a citizen of Britain than of any other country in the world"
		label variable nat3 "There are some things about Britain today that make me ashamed to be British"
		label variable nat4 "People in Britain are too ready to criticise their country"
		label variable nat5 "The government should do everything it can to keep all parts of Britain together in a single state"
		label variable nat6 "Britain should co-operate with other countries, even if it means giving up some independence"
		 
		label define agreescale 0 "strongly disagree" 4 "strongly agree"
		label values nat1 agreescale 
		label values nat2 agreescale 
		label values nat3 agreescale 
		label values nat4 agreescale 
		label values nat5 agreescale 
		label values nat6 agreescale 
	
		
		* Main nationalism factor (based on polychoric factor analysis)
		polychoric nat2 nat4 nat6
		display r(sum_w)
		global N = r(sum_w)
		matrix r = r(R)
		factormat r, n($N) pcf
		predict nationalism		

	
* recode EUROPEAN MEMBERSHIP SUPPORT items
		recode opeur1 (-9=.) (-8=.) (-7=.) (-2=.) (-1=.) (1=2) (2=0) (3=1), gen(eu1)  // Generally speaking, do you think that Britain's membership of the European Union is a good thing, a bad thing or is it neither good nor bad?
		recode opeur2 (-9=.) (-8=.) (-7=.) (-2=.) (-1=.) (1=1) (2=0), gen(eu2)  // Taking everything into consideration, would you say that Britain has on balance benefited or not from being a member of the European Union?
		recode opeur3 (-9=.) (-8=.) (-7=.) (-2=.) (-1=.) (1=0) (2=1) (3=2) (4=3) (5=4) , gen(eu3) // Do you think Britain's long-term policy should be... 
			 // To leave the European Union / To stay in the EU and try to reduce the EU's powers / To leave things as they are / To stay in the EU and try and increase the EU's powers / or To work for the formation of a single European government? 
		recode opeur4 (-9=.) (-8=.) (-7=.) (-2=.) (-1=.), gen(eu4) // If there were a referendum on whether Britain should join the single European currency, the Euro, how do you think you would vote? 
			// Would you vote to join the Euro, or not to join the Euro?

		label variable eu1 "desirability of eu membership"
		label variable eu2 "membership of eu beneficial"
		label variable eu3 "long term eu policy"
		label variable eu4 "voting intention in euro referendum"


		* EU support factor (based on polychoric factor analysis)
		polychoric eu1 eu2 eu3
		display r(sum_w)
		global N = r(sum_w)
		matrix r = r(R)
		factormat r, n($N) pcf
		predict EUsupport

		
		
*** Compute changes in nationalist attitudes/EU membership support	
		sort pid waven
		
		* nationalism
		gen D3_nationalism=nationalism-L3.nationalism
		
		* EU support
		gen LDV_EUsupport=L3.EUsupport if year==2002
		replace LDV_EUsupport=L4.EUsupport if year==2006

		gen D_EUsupport=EUsupport-LDV_EUsupport
	
		
* recode economic policy items
		bysort waven: sum opsoca opsocb opsocc opsocd opsoce opsocf
		* People have different views about society. I'm going to read out some things people have said about Britain today and I'd like you to tell me which answer off the card comes closest to how you feel about each statement.
		recode opsoca (-9=.) (-8=.) (-7=.) (-2=.) (-1=.) (1=4) (2=3) (3=2) (4=1) (5=0), gen(econpol1) // Ordinary people get their fair share of the nation's wealth
		recode opsocb (-9=.) (-8=.) (-7=.) (-2=.) (-1=.) (1=4) (2=3) (3=2) (4=1) (5=0), gen(econpol2) // There is one law for the rich and one for the poor
		recode opsocc (-9=.) (-8=.) (-7=.) (-2=.) (-1=.) (1=4) (2=3) (3=2) (4=1) (5=0), gen(econpol3) // Private enterprise is the best way to solve Britain's economic problems
		recode opsocd (-9=.) (-8=.) (-7=.) (-2=.) (-1=.) (1=4) (2=3) (3=2) (4=1) (5=0), gen(econpol4) // Major public services and industries ought to be in state ownership
		recode opsoce (-9=.) (-8=.) (-7=.) (-2=.) (-1=.) (1=4) (2=3) (3=2) (4=1) (5=0), gen(econpol5) // It is the government's responsibility to provide a job for everyone who wants one
		recode opsocf (-9=.) (-8=.) (-7=.) (-2=.) (-1=.) (1=4) (2=3) (3=2) (4=1) (5=0), gen(econpol6) // Strong trade unions are needed to protect the working conditions and wages of employees

	
			

***************** CONTROL VARIABLES ********************************************
********************************************************************************
		
*** socio-demographics 
		* Gender
		fre sex 
		recode sex (-7=.) (-9=.) (1=1) (2=0), gen(male)
		label variable male "Male"
		label define male 0 "female" 1 "male"
		label values male male	
		
		
		* Age
		mvdecode (age), mv(-9)
		
		
		* Divide age by hundred
		gen age_cent=age/100
		label variable age_cent "age"
			
			
		* Highest educational qualification
		mvdecode (qfedhi), mv(-9 -7)
		recode qfedhi (13=.) (12=0) (8/11=1) (7=2) (6=3) (3/5=4) (1 2=5), gen(education)
		label define education 0 "No qualification" 1 "Other qualification" 2 "GCSE etc" 3 "A-level etc" 4 "Other higher degree" 5 "Degree"
		label values education education

		
		* Migration background
			* own place of birth
			fre plbornc 
			gen bornelsewhere=0 if plbornc==-8
			replace bornelsewhere=1 if plbornc>0 
			
			sort pid wave
			bysort pid: replace bornelsewhere=1 if bornelsewhere[_n-1]==1
			
			bysort pid: egen bornelsewhere2=max(bornelsewhere)

			
			* Parents
				* Father born
				gen fatherbornelsewhere=1 if pabrn>0 & pabrn<.
				replace fatherbornelsewhere=0 if pabrn==104 // England
				replace fatherbornelsewhere=0 if pabrn==316 // Wales
				replace fatherbornelsewhere=0 if pabrn==257 // Scotland
				replace fatherbornelsewhere=0 if pabrn==153 // Northern Ireland

				label define parent_born2 0 "elsewhere" 1 "england, wales, scotland, north. ireland" 
				// label define parent_born 0 "elsewhere" 1 "england" 2 "wales" 3 "scotland" 4 "northern ireland"
				label values fatherborn parent_born2

				bysort pid: egen fatherbornelsewhere2=max(fatherbornelsewhere)
				
				* Mother born
				gen motherbornelsewhere=1 if mabrn>0 & mabrn<.
				replace motherbornelsewhere=0 if mabrn==104 // England
				replace motherbornelsewhere=0 if mabrn==316 // Wales
				replace motherbornelsewhere=0 if mabrn==257 // Scotland
				replace motherbornelsewhere=0 if mabrn==153 // Northern Ireland

				label values motherborn parent_born2

				bysort pid: egen motherbornelsewhere2=max(motherbornelsewhere)

				* summarize for both parents
				gen parentbornelsewhere_both=1 if fatherbornelsewhere2==1 & motherbornelsewhere2==1
				replace parentbornelsewhere_both=0 if fatherbornelsewhere2==0 | motherbornelsewhere2==0
				
				gen parentbornelsewhere_any=1 if fatherbornelsewhere2==1 | motherbornelsewhere2==1
				replace parentbornelsewhere_any=0 if fatherbornelsewhere2==0 & motherbornelsewhere2==0
				
				gen parentbornelsewhere_one=0 if parentbornelsewhere_both!=.
				replace parentbornelsewhere_one=1 if parentbornelsewhere_any==1 & parentbornelsewhere_both==0

				gen parentsbornelsewhere=0 if parentbornelsewhere_both!=.
				replace parentsbornelsewhere=1 if parentbornelsewhere_any==1 & parentbornelsewhere_both==0
				replace parentsbornelsewhere=2 if parentbornelsewhere_both==1

				label define parentsbornelsewhere 0 "none" 1 "one" 2 "both"
				label values parentsbornelsewhere parentsbornelsewhere
	
		
		* current labour force status
		mvdecode (jbstat), mv(-9 -8 -7 -1 -2)
		fre jbstat
		recode jbstat (3=1) (1 2 4 5 6 7 8 9 10=0), gen(unemployed)
		
		recode jbstat (0=1) (2=0) (3=2) (4=3) (6=4) (5 7 8 9 10=5), gen(jbstat2)
		label define jbstat2 0 "in paid employment" 1 "self employed" 2 "unemployed" 3 "retired" 4 "student" 5 "other inactive" 
		label values jbstat2 jbstat2
							
		recode jbstat (0=0) (2=0) (3=0) (4=1) (6=1) (5 7 8 9 10=1), gen(jbstat3)
		label define jbstat3 0 "active" 1 "inactive"
		label values jbstat3 jbstat3			
		
			
		* job status: retired
		recode jbstat (1 2 3 5 6 7 8 9 10=0) (4=1), gen(retired)
		

		
******************************  OCCUPATION *************************************
********************************************************************************
*** SIC 1992
* our research assistants created a do-file that includes the labels of SIC 1992 as listed in the codebook ("bhpsvola.pdf", see p. 287 and following)
		do "SIC92label.do"
		label list jbsic1992
		label values jbsic92 jbsic1992
		tab jbsic92, nol 
		fre jbsic92
		codebook jbsic92 // 720 unique values, 101 not labelled 
				// most values are indeed labelled	
				// unlabelled values have little observations (but see 9990 and 9998)
				// in some cases labels are missing because entries have apparently not been filled up with zeros (as was done in all other cases); these should be 1-> 100; 410 -> 4100; 802 -> 8020; 853 -> 8530)
				// others are just unknown in the codebook (e.g. 1526; 1553; 1743, 2421, 2424)- and some of these might just be coding errors (e.g. 810 could be 801)

* aggregate to three digit/delete last digit
		gen long jbsic92_3dig = floor(jbsic92/10)
		replace jbsic92_3dig = jbsic92_3dig*10
		label values jbsic92_3dig jbsic1992
		tab jbsic92_3dig
		fre jbsic92_3dig
		codebook jbsic92_3dig // 312 unique values, 53 not labelled

* aggregate to two digit/delete last two digits
		gen long jbsic92_2dig = floor(jbsic92/100)
		replace jbsic92_2dig = jbsic92_2dig*100
		label values jbsic92_2dig jbsic1992
		tab jbsic92_2dig
		codebook jbsic92_2dig // 81 unique values, 23 not labelled

* create sic variable at the section (n=17) level		
		gen jbsic92_section=. if jbsic92_2dig==-100 | jbsic92_2dig==0 | jbsic92_2dig==400 | jbsic92_2dig==800 | jbsic92_2dig==4200 | jbsic92_2dig==4600 | jbsic92_2dig==4700 | jbsic92_2dig==4800 | ///
								jbsic92_2dig==5300 | jbsic92_2dig==5400 | jbsic92_2dig==5600 | jbsic92_2dig==5800 | jbsic92_2dig==7700 | jbsic92_2dig==7800 | jbsic92_2dig==7900 | jbsic92_2dig==8100 | ///
								jbsic92_2dig==8200 | jbsic92_2dig==8300 | jbsic92_2dig==8400 | jbsic92_2dig==8600 | jbsic92_2dig==8900 | jbsic92_2dig==9400 | jbsic92_2dig==9600  // these are all codes that shouldn't exist according to the codebook, likely data entry errors

		replace jbsic92_section=1 if jbsic92_2dig==100 | jbsic92_2dig==200 // AGRICULTURE, HUNTING AND FORESTRY
		replace jbsic92_section=2 if jbsic92_2dig==500 // FISHING
		replace jbsic92_section=3 if jbsic92_2dig==1000 | jbsic92_2dig==1100 | jbsic92_2dig==1400 // MINING AND QUARRYING
		replace jbsic92_section=4 if jbsic92_2dig>=1500 & jbsic92_2dig<=3700 // MANUFACTURING
		replace jbsic92_section=5 if jbsic92_2dig==4000 | jbsic92_2dig==4100 // ELECTRICITY, GAS AND WATER SUPPLY
		replace jbsic92_section=6 if jbsic92_2dig==4500 // CONSTRUCTION
		replace jbsic92_section=7 if jbsic92_2dig==5000 | jbsic92_2dig==5100 | jbsic92_2dig==5200 // WHOLESALE AND RETAIL TRADE; REPAIR OF MOTOR VEHICLES, MOTORCYCLES AND PERSONAL AND HOUSEHOLD GOODS
		replace jbsic92_section=8 if jbsic92_2dig==5500 // HOTELS AND RESTAURANTS
		replace jbsic92_section=9 if jbsic92_2dig>=6000 & jbsic92_2dig<=6400 // TRANSPORT, STORAGE AND COMMUNICATION
		replace jbsic92_section=10 if jbsic92_2dig>=6500 & jbsic92_2dig<=6700 // FINANCIAL INTERMEDIATION
		replace jbsic92_section=11 if jbsic92_2dig>=7000 & jbsic92_2dig<=7400 // REAL ESTATE, RENTING AND BUSINESS ACTIVITIES
		replace jbsic92_section=12 if jbsic92_2dig==7500 // PUBLIC ADMINISTRATION AND DEFENCE; COMPULSORY SOCIAL SECURITY
		replace jbsic92_section=13 if jbsic92_2dig==8000 // EDUCATION
		replace jbsic92_section=14 if jbsic92_2dig==8500 // HEALTH AND SOCIAL WORK
		replace jbsic92_section=15 if jbsic92_2dig>=9000 & jbsic92_2dig<=9300 // OTHER COMMUNITY, SOCIAL AND PERSONAL SERVICE ACTIVITIES
		replace jbsic92_section=16 if jbsic92_2dig==9500 // PRIVATE HOUSEHOLDS WITH EMPLOYED PERSONS
		replace jbsic92_section=17 if jbsic92_2dig==9900 // EXTRA-TERRITORIAL ORGANISATIONS AND BODIES

		label define jbsic92_section 1 "AGRICULTURE" 2 "FISHING" 3 "MINING" 4 "MANUFACTURING" 5 "ELECTRICITY" 6 "CONSTRUCTION" 7 "WHOLESALE & RETAIL" 8 "HOTELS & RESTAURANTS" ///
									  9 "TRANSPORT, STORAGE & COMMUNICATION" 10 "FINANCE" 11 "REAL ESTATE" 12 "PUBLIC ADMINISTRATION & DEFENCE"	13 "EDUCATION" ///
									  14 "HEALTH & SOCIAL WORK" 15 "OTHER SERVICE" 16 "PRIVATE HOUSEHOLDS" 17 "EXTRA-TERRITORIAL ORGANISATIONS"
		label values jbsic92_section jbsic92_section

		clonevar jbsic_section_manufdisagg=jbsic92_section
		replace jbsic_section_manufdisagg=jbsic92_2dig if jbsic92_section==4
		label define jbsic_section_manufdisagg 1 "AGRICULTURE" 2 "FISHING" 3 "MINING" 4 "MANUFACTURING" 5 "ELECTRICITY" 6 "CONSTRUCTION" 7 "WHOLESALE & RETAIL" 8 "HOTELS & RESTAURANTS" ///
									  9 "TRANSPORT, STORAGE & COMMUNICATION" 10 "FINANCE" 11 "REAL ESTATE" 12 "PUBLIC ADMINISTRATION & DEFENCE"	13 "EDUCATION" ///
									  14 "HEALTH & SOCIAL WORK" 15 "OTHER SERVICE" 16 "PRIVATE HOUSEHOLDS" 17 "EXTRA-TERRITORIAL ORGANISATIONS" ///
									  1500 "MANUFACTURE OF FOOD PRODUCTS AND BEVERAGES" 1600 "MANUFACTURE OF TOBACCO PRODUCTS" 1700	"MANUFACTURE OF TEXTILES" ///
									  1800	"MANUFACTURE OF WEARING APPAREL; DRESSING AND DYING OF FUR" 1900 "TANNING AND DRESSING OF LEATHER; MANUFACTURE OF LUGGAGE, HANDBAGS, SADDLERY, HARNESS AND FOOTWEAR" ///
									  2000	"MANUFACTURE OF WOOD AND OF PRODUCTS OF WOOD AND CORK, EXCEPT FURNITURE; MANUFACTURE OF ARTICLES OF STRAW AND PLAITING MATERIALS" ///
									  2100	"MANUFACTURE OF PULP, PAPER AND PAPER PRODUCTS" ///
									  2200	"PUBLISHING, PRINTING AND REPRODUCTION OF RECORDED MEDIA" ///
									  2300	"MANUFACTURE OF COKE, REFINED PETROLEUM PRODUCTS" ///
									  2400	"MANUFACTURE OF CHEMICALS AND CHEMICAL PRODUCTS" ///
									  2500	"MANUFACTURE OF RUBBER AND PLASTIC PRODUCTS" ///
									  2600	"MANUFACTURE OF OTHER NON-METALLIC MINERAL PRODUCTS" ///
									  2700	"MANUFACTURE OF BASIC METALS" ///
									  2800	"MANUFACTURE OF FABRICATED METAL PRODUCTS, EXCEPT MACHINERY AND EQUIPMENT" ///
									  2900	"MANUFACTURE OF MACHINERY AND EQUIPMENT NOT ELSEWHERE CLASSIFIED" ///
									  3000	"MANUFACTURE OF OFFICE MACHINERY AND COMPUTERS" ///
									  3100	"MANUFACTURE OF ELECTRICAL MACHINERY AND APPARATUS NOT ELSEWHERE CLASSIFIED" ///
									  3200	"MANUFACTURE OF RADIO, TELEVISION AND COMMUNICATION EQUIPMENT AND APPARATUS" ///
									  3300	"MANUFACTURE OF MEDICAL, PRECISION AND OPTICAL INSTRUMENTS, WATCHES AND CLOCKS" ///
									  3400	"MANUFACTURE OF MOTOR VEHICLES, TRAILERS AND SEMITRAILERS" ///
									  3500	"MANUFACTURE OF OTHER TRANSPORT EQUIPMENT" ///
									  3600	"MANUFACTURE OF FURNITURE; MANUFACTURING NOT ELSEWHERE CLASSIFIED" ///
									  3700	"RECYCLING" 
		label values jbsic_section_manufdisagg jbsic_section_manufdisagg
		
*** SIC 1980
	rename jbsic jbsic80
* run do-file that includes the labels of SIC 1980 as listed in the BHPS codebook ("bhpsvola.pdf", see p. 276 and following)
		do "SIC80label.do"
		label list jbsic1980
		label values jbsic80 jbsic1980
		tab jbsic80
		codebook jbsic80 // 550 unique values, 157 not labelled
			
* aggregate to three digit/delete last digit
		gen long jbsic80_3dig = floor(jbsic80/10)
		replace jbsic80_3dig = jbsic80_3dig*10
		label values jbsic80_3dig jbsic1980
		tab jbsic80_3dig
		codebook jbsic80_3dig // 339 unique values, 53 not labelled

* aggregate to four digit/delete last two digits
		gen long jbsic80_2dig = floor(jbsic80/100)
		replace jbsic80_2dig = jbsic80_2dig*100
		label values jbsic80_2dig jbsic1980
		tab jbsic80_2dig
		codebook jbsic80_2dig // 84 unique values, 24 not labelled